package com.qf.mybatisplus.rabbitMQ;


import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.core.Message;
//消费者
//@RabbitListener(queues = "simpleQueue") 在类上使用 @RabbitListener  在方法上 使用@RabbitHandler 这样只能监听一个队列
//@Component
public class HelloConsumer {



    // 方式1   在配置类中 创建 queue   在这里引用即可
    @RabbitListener(queues = "simpleQueue", containerFactory="rabbitListenerContainerFactory")
    public void receive1(@Payload String msg) throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        System.out.println("收到: "+msg);
    }



    //另外,@RabbitListener 注解中也可以直接定义队列:  如下
    //  @RabbitListener(queuesToDeclare = @Queue(name = "helloworld",durable = "false"))
    // 方式2   在注解上   创建queue
    //@RabbitListener(queuesToDeclare = @Queue(name = "helloQueue",durable = "false",autoDelete = "true"))   自定义属性
    @RabbitListener(queuesToDeclare = @Queue("helloQueue"))   // 默认属性   durable = "true" 持久化  exclusive="false" 非独占   autoDelete = "false"不自动删除
    public void receive2(String msg, Channel channel, Message message) throws InterruptedException, IOException {
        TimeUnit.SECONDS.sleep(1);
        System.out.println("消费者2收到: "+msg);
        // 手动  ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = "boot-queue")
    public void getMessage(Object message){
        System.out.println("接收到消息：" + message);
    }

}
